import java.util.*;

/**
 * @author dave
 *
 */
public class Item {
	private Item[] previous;
	private Item[] next;
	private String data;
	
	//height is the max height, so values go 0, 1, 2, ... n
	private int height;
	private int key;
	
	public Item(int _key, String _data){
		this(_key, _data, generateHeight());
	}
	public Item(int _key, String _data, int _height){
		height = _height;
		key = _key;
		data = _data;
		previous = new Item[height+1];
		next = new Item[height+1];
	}
	
	public String getData() {
		return data;
	}
	public void setData(String data) {
		this.data = data;
	}
	public int getHeight() {
		return height;
	}
	
	// 	Sets the height to the passed int and copies 
	//	the references to the new array
	public void setHeight(int newHeight){
		Item[] tempPrevious = previous;
		Item[] tempNext = next;
		previous = new Item[newHeight+1];
		next = new Item[newHeight+1];
		
		if(newHeight > height){
			for(int i=0; i<=height; i++){
				previous[i] = tempPrevious[i];
				next[i] = tempNext[i];
			}
		}else{
			for(int i=0; i<=newHeight; i++){
				previous[i] = tempPrevious[i];
				next[i] = tempNext[i];
			}
		}
		height = newHeight;
	}
	
	public int getKey() {
		return key;
	}
	public void setKey(int key) {
		this.key = key;
	}
	public Item getNext(int h) {
		return next[h];
	}
	public void setNext(int h, Item next) {
		this.next[h] = next;
	}
	public Item getPrevious(int h) {
		return previous[h];
	}
	public void setPrevious(int h, Item previous) {
		this.previous[h] = previous;
	}
	
	
	static Random randseq;
	
	private static int generateHeight(){
		if(randseq==null)
			randseq = new Random();
		int height = 0;
		while(randseq.nextInt(2)==0)
			height++;
		return height;
	}
	
	public String toString(){
		return key+": "+data;
	}
	
}
